package nju.bishe.weibo;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import nju.bishe.weibo.data.Tweet;
import nju.bishe.weibo.data.db.StatusTable;
import nju.bishe.weibo.ui.base.TwitterCursorActivity;
import weibo4android.Paging;
import weibo4android.Status;
import weibo4android.WeiboException;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;

//TODO: 数据来源换成 getFavorites()
public class FavoritesActivity extends TwitterCursorActivity {
        private static final String TAG = "FavoritesActivity";

        private static final String LAUNCH_ACTION = "nju.bishe.weibo.FAVORITES";
        private static final String USER_ID = "userid";

        static final int DIALOG_WRITE_ID = 0;
        
        private String userId = null;
        private int mNextPage=1;
        public static Intent createIntent(String userId) {
                Intent intent = new Intent(LAUNCH_ACTION);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra(USER_ID, userId);

                return intent;
        }

        @Override
        protected boolean _onCreate(Bundle savedInstanceState) {
                if (super._onCreate(savedInstanceState)){
                        setHeaderTitle(getActivityTitle());
                        
                        return true;
                }else{
                        return false;
                }

        }

        public static Intent createNewTaskIntent(String userId) {
                Intent intent = createIntent(userId);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                return intent;
        }

        // Menu.
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                return super.onCreateOptionsMenu(menu);
        }

        @Override
        protected Cursor fetchMessages() {
                // TODO Auto-generated method stub
                return getDb().fetchAllTweets(getUserId(), StatusTable.TYPE_FAVORITE);
        }

        @Override
        protected String getActivityTitle() {
                // TODO Auto-generated method stub
                String template = getString(R.string.page_title_favorites);
                String who;
                if (getUserId().equals(TwitterApplication.getMyselfId())){
                        who = "我";
                }else{
                        who = getUserId();
                }
                return MessageFormat.format(template, who);
        }

        
        @Override
        protected void markAllRead() {
                // TODO Auto-generated method stub
                getDb().markAllTweetsRead(getUserId(), StatusTable.TYPE_FAVORITE);
        }
        
        
        // hasRetrieveListTask interface
        
        @Override
        public int addMessages(ArrayList<Tweet> tweets, boolean isUnread) {
                return getDb().putTweets(tweets, getUserId(), StatusTable.TYPE_FAVORITE, isUnread);
        }
        
        @Override
        public String fetchMaxId() {
                return getDb().fetchMaxTweetId(getUserId(), StatusTable.TYPE_FAVORITE);
        }

        @Override
        public List<Status> getMessageSinceId(String maxId) throws WeiboException {
                if (maxId != null){
                        return getApi().getFavorites(mNextPage);
                }else{
                        return getApi().getFavorites(mNextPage);
                }
        }

        @Override
        public String fetchMinId() {
                return getDb().fetchMinTweetId(getUserId(), StatusTable.TYPE_FAVORITE);
        }

        @Override
        public List<Status> getMoreMessageFromId(String minId)
                        throws WeiboException {
                mNextPage++;
                //Paging paging = new Paging(1, 20);
                //paging.setMaxId(Integer.parseInt(minId));
                return getApi().getFavorites(mNextPage);
        }

        @Override
        public int getDatabaseType() {
                return StatusTable.TYPE_FAVORITE;
        }

        @Override
        public String getUserId() {
                Intent intent = getIntent();
                Bundle extras = intent.getExtras();
                if (extras != null){
                        userId = extras.getString(USER_ID);
                } else {
                        userId = TwitterApplication.getMyselfId();
                }

                return userId;
                
        }

}

